package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.EventDAO;
import com.rainng.coursesystem.dao.AreaDAO;
import com.rainng.coursesystem.dao.LeaderDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.AreaEntity;
import com.rainng.coursesystem.model.entity.LeaderEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.LeaderItemVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class LeaderManager extends BaseManager {
    private final AreaDAO areaDAO;
    private final LeaderDAO leaderDAO;
    private final EventDAO eventDAO;

    public LeaderManager(AreaDAO areaDAO, LeaderDAO leaderDAO, EventDAO eventDAO) {
        this.areaDAO = areaDAO;
        this.leaderDAO = leaderDAO;
        this.eventDAO = eventDAO;
    }

    public Integer getPageCount(String areaName, String name) {
        int count = leaderDAO.count(areaName, name);
        return calcPageCount(count, LeaderDAO.PAGE_SIZE);
    }

    public List<LeaderItemVO> getPage(Integer index, String areaName, String name) {
        return leaderDAO.getPage(index, areaName, name);
    }

    public LeaderEntity get(Integer id) {
        return leaderDAO.get(id);
    }

    public int create(LeaderEntity entity) {
        return leaderDAO.insert(entity);
    }

    public int update(LeaderEntity entity) {
        return leaderDAO.update(entity);
    }

    public int delete(Integer id) {
        return leaderDAO.delete(id);
    }

    public boolean hasEvent(Integer leaderId) {
        return eventDAO.countByLeaderId(leaderId) > 0;
    }

    public AreaEntity getAreaById(Integer id) {
        return areaDAO.get(id);
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<LeaderEntity> entityList = leaderDAO.listName();
        for (LeaderEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
